﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ComLib.Lang.Core;
using ComLib.Lang.Types;
using FluentScript.IDE.Lib;
using ComLib.Lang;


namespace FluentScript.IDE.Parts
{

    /// <summary>
    /// Represents the UI for showing the local variables in the script.
    /// </summary>
    public class Locals : ViewPartBase
    {
        private TextBox _grid;


        public Locals(TextBox grid)
        {
            _grid = grid;
        }


        /// <summary>
        /// Updates the ui.
        /// </summary>
        public override void Update()
        {
            //    _grid.AutoGenerateColumns = true;
            //    _grid.ItemsSource = null;
            //    _grid.IsReadOnly = true;
            _grid.Text = string.Empty;
        
            // Convert variables in current block into result objects and databind.
            var results = new List<Result>();
            Context.Ip.Memory.Each(pair =>
            {
                object val = ((Variable)pair.Value).Value;
                var actualVal = ((LObject) val).GetValue();
                string type = val == null ? string.Empty : ((LObject)val).Type.Name;
                if (((LObject)val).Type == LTypes.Array)
                    actualVal = GetListValues(((LArray) val).Value);
                type = ToFriendlyTypeName(type);
                results.Add(new Result() { Name = pair.Key, Value = actualVal, Type = type });
            });
            string content = "";
            foreach (var result in results)
            {
                content += result.ToString() + Environment.NewLine;
            }
            _grid.Text = content;
        }


        /// <summary>
        /// Clears the UI.
        /// </summary>
        public override void Clear()
        {
            _grid.Text = string.Empty;
        }
        


        private string ToFriendlyTypeName(string type)
        {
            if (type == "String") return "text";
            if (type == "Double") return "number";
            if (type == "Boolean") return "yes/no";
            if (type == "DateTime") return "date";
            if (type == "TimeSpan") return "time";
            if (type == "int") return "number";
            return type;
        }

        
        private string GetListValues(IList items)
        {
            var text = "";
            for(var ndx = 0; ndx < items.Count; ndx++)
            {
                text += ((LObject) items[ndx]).GetValue().ToString();
                if (ndx < items.Count - 1)
                    text += ", ";
            }
            return text;
        }
    }


    public class Result
    {
        public string Name { get; set; }
        public object Value { get; set; }
        public string Type { get; set; }


        public override string ToString()
        {
            return Name + " : " + Value;
        }
    }
}
